package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.CourseChangesApplicationVO;
import edu.zqu.instructionalMatters.project.entity.CourseChangesApplication;
import edu.zqu.instructionalMatters.project.mapper.CourseChangesApplicationMapper;
import edu.zqu.instructionalMatters.project.util.CourseChangesApplicationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课程变动申请 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "课程变动申请")
public class CourseChangesApplicationController {
    @Autowired
    private CourseChangesApplicationMapper courseChangesApplicationMapper;
    @Autowired
    private CourseChangesApplicationUtil courseChangesApplicationUtil;

    @ApiOperation(value = "获取课程变动申请列表")
    @RequestMapping(value = "/courseChangesApplications", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = false),
            @ApiImplicitParam(name = "applicationType", value = "课程变动申请类型id", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             BigInteger teacherId,
                             Integer applicationType) {
        CourseChangesApplication courseChangesApplication1 = new CourseChangesApplication()
                .setCollegeId(collegeId).setTeacherId(teacherId).setApplicationType(applicationType);                                             //合成对象
        List<CourseChangesApplication> list1 = courseChangesApplicationMapper.findAll(courseChangesApplication1);                                 //获取List<CourseChangesApplication>
        List<CourseChangesApplicationVO> list = new ArrayList<>();                                                                                //初始化List<CourseChangesApplicationVO>
        for (CourseChangesApplication courseChangesApplication : list1) {                                                                          //循环遍历
            CourseChangesApplicationVO courseChangesApplicationVO = courseChangesApplicationUtil.courseChangesApplicationUtil(courseChangesApplication);
            list.add(courseChangesApplicationVO);                                                                                                  //courseChangesApplicationVO 加入list
        }
        return ZQUResult.success(list);                                                                                                            //数据返回
    }

    @ApiOperation(value = "获取课程变动申请信息", response = CourseChangesApplicationVO.class)
    @GetMapping(value = "/courseChangesApplication/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        CourseChangesApplication courseChangesApplication = courseChangesApplicationMapper.selectById(id);                                    //课程变动申请对象
        CourseChangesApplicationVO courseChangesApplicationVO = courseChangesApplicationUtil.courseChangesApplicationUtil(courseChangesApplication);
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", courseChangesApplicationVO);                                                //数据返回
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/courseChangesApplication", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "applicationType", value = "申请类型id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "timeTableId", value = "作息id", paramType = "query", required = false),
            @ApiImplicitParam(name = "newTimeTableId", value = "修改后作息id", paramType = "query", required = false),
            @ApiImplicitParam(name = "calendarId", value = "校历id", paramType = "query", required = false),
            @ApiImplicitParam(name = "newCalendarId", value = "修改后校历id", paramType = "query", required = false),
            @ApiImplicitParam(name = "detail", value = "详情，原因", paramType = "query", required = true),
            @ApiImplicitParam(name = "telephone", value = "联系方式", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger teacherId,
                         Integer applicationType,
                         BigInteger curriculumId,
                         BigInteger timeTableId,
                         BigInteger newTimeTableId,
                         BigInteger calendarId,
                         BigInteger newCalendarId,
                         String detail,
                         String telephone,
                         BigInteger byWho) {
        CourseChangesApplication courseChangesApplication = new CourseChangesApplication();               //创建课程变动申请数据模型
        courseChangesApplication.setCollegeId(collegeId).setTeacherId(teacherId);                         //设置学校id、教师id
        courseChangesApplication.setApplicationType(applicationType).setCurriculumId(curriculumId);       //设置课程变动申请类型id、课表id
        courseChangesApplication.setTimeTableId(timeTableId).setNewTimeTableId(newTimeTableId);           //设置原作息id、变更后作息id
        courseChangesApplication.setCalendarId(calendarId).setNewCalendarId(newCalendarId);               //设置原校历id、变更后校历id
        courseChangesApplication.setDetail(detail).setTelephone(telephone).setByWho(byWho);               //设置详情（原因）、申请人联系电话、操作者
        courseChangesApplicationMapper.insert(courseChangesApplication);                                  //执行插入
        return ZQUResult.success();                                                                       //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/courseChangesApplication/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "applicationType", value = "申请类型id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "timeTableId", value = "作息id", paramType = "query", required = false),
            @ApiImplicitParam(name = "newTimeTableId", value = "修改后作息id", paramType = "query", required = false),
            @ApiImplicitParam(name = "calendarId", value = "校历id", paramType = "query", required = false),
            @ApiImplicitParam(name = "newCalendarId", value = "修改后校历id", paramType = "query", required = false),
            @ApiImplicitParam(name = "detail", value = "详情，原因", paramType = "query", required = true),
            @ApiImplicitParam(name = "telephone", value = "联系方式", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger teacherId,
                            Integer applicationType,
                            BigInteger curriculumId,
                            BigInteger timeTableId,
                            BigInteger newTimeTableId,
                            BigInteger calendarId,
                            BigInteger newCalendarId,
                            String detail,
                            String telephone,
                            BigInteger byWho) {
        CourseChangesApplication courseChangesApplication = courseChangesApplicationMapper.selectById(id);//查出来
        courseChangesApplication.setByWho(byWho);                                                         //设置操作者
        courseChangesApplicationMapper.updateById(courseChangesApplication);                              //更新操作者
        courseChangesApplicationMapper.deleteById(id);                                                    //逻辑删除
        courseChangesApplication = new CourseChangesApplication();                                         //创建课程变动申请数据模型
        courseChangesApplication.setCollegeId(collegeId).setTeacherId(teacherId);                         //设置学校id、教师id
        courseChangesApplication.setApplicationType(applicationType).setCurriculumId(curriculumId);       //设置课程变动申请类型id、课表id
        courseChangesApplication.setTimeTableId(timeTableId).setNewTimeTableId(newTimeTableId);           //设置原作息id、变更后作息id
        courseChangesApplication.setCalendarId(calendarId).setNewCalendarId(newCalendarId);               //设置原校历id、变更后校历id
        courseChangesApplication.setDetail(detail).setTelephone(telephone);                               //设置详情（原因）、申请人联系电话
        courseChangesApplication.setByWho(byWho).setLastVersion(id);                                      //设置操作者、上一个版本
        courseChangesApplicationMapper.insert(courseChangesApplication);                                  //执行插入
        return ZQUResult.success();                                                                       //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/courseChangesApplication/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        CourseChangesApplication courseChangesApplication = courseChangesApplicationMapper.selectById(id);//查出来
        courseChangesApplication.setByWho(byWho);                                                         //设置操作者
        courseChangesApplicationMapper.updateById(courseChangesApplication);                              //更新操作者
        courseChangesApplicationMapper.deleteById(id);                                                    //逻辑删除
        return ZQUResult.success();                                                                       //操作成功返回
    }
}

